001 /* EVolve - an Extensible Software Visualization Framework
002 * Copyright (C) 2001-2002 Qin Wang
003 *
004 * This library is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU Library General Public
006 * License as published by the Free Software Foundation; either
007 * version 2 of the License, or (at your option) any later version.
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Library General Public License for more details.
013 *
014 * You should have received a copy of the GNU Library General Public
015 * License along with this library; if not, write to the
016 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017 * Boston, MA 02111-1307, USA.
018 */
019
020 /*
021 * EVolve is distributed at http://www.sable.mcgill.ca/EVolve/
022 */
023
024 package EVolve.data;
025
026 import java.util.HashMap;
027
028 public class ValueComparator implements EntityComparator, Cloneable {
029 private String name;
030 private boolean increase;
031 private int[] value;
032 private HashMap EntityId2Int;
033
034 public ValueComparator(String name, boolean increase, int[] value, HashMap EntityId2Int) {
035 this.name = name;
036 this.increase = increase;
037 this.value = value;
038 this.EntityId2Int = EntityId2Int;
039 }
040
041 public String getName() {
042 return name;
043 }
044
045 public int compare(Entity entity1, Entity entity2) {
046 int entityId1 = ((Integer)EntityId2Int.get(entity1.getName())).intValue();
047 int entityId2 = ((Integer)EntityId2Int.get(entity2.getName())).intValue();
048 if (increase) {
049 return value[entityId1] - value[entityId2];
050 } else {
051 return value[entityId2] - value[entityId1];
052 }
053 }
054
055 public Object clone() {
056 ValueComparator o = null;
057 try {
058 o = (ValueComparator)super.clone();
059 } catch (CloneNotSupportedException e) {
060 return null;
061 }
062 o.name = name;
063 if (value != null) {
064 o.value = new int[value.length];
065 for (int i=0; i<value.length; i++)
066 o.value[i] = value[i];
067 }
068 return o;
069 }
070 }